﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Deployment.Application;
using System.IO;
using Util;
using EZBarTender_Wrapper.SqlExecutor;

namespace EZBarTender_Wrapper.Settings
{
    public partial class SettingsConfig : Form
    {
        private String localAppData;
        private String userFilePath;
        private ApplicationConfig appConfig;

        public SettingsConfig()
        {
            InitializeComponent();

            appConfig = new ApplicationConfig(Properties.Settings.Default);
            propertyGrid.SelectedObject = appConfig;

            localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            userFilePath = Path.Combine(localAppData, "POSPlus");

            //propertyGrid.SelectedObject = Properties.Settings.Default;
            //System.Configuration.UserScopedSettingAttribute userAttr = new System.Configuration.UserScopedSettingAttribute();
            //System.ComponentModel.AttributeCollection attrs = new AttributeCollection(userAttr);
            //propertyGrid.BrowsableAttributes = attrs;
             
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btwFileDialog.InitialDirectory = Path.Combine(userFilePath, "labels");
            btwFileDialog.ShowDialog();
            String[] files = btwFileDialog.FileNames;
            foreach (String file in files)
            {
                System.Diagnostics.Process.Start(file);
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            Console.WriteLine(Properties.Settings.Default.LocalConnectionString);
            appConfig.Save();
            DialogResult result = 
                MessageBox.Show(
                    this, 
                    "To apply changes you must restart EZBarTender.\nDo you want to restart now?",
                    "Restart EZBarTender",
                    MessageBoxButtons.YesNo, 
                    MessageBoxIcon.Information, 
                    MessageBoxDefaultButton.Button1);

            if (result == DialogResult.Yes)
            {
                Application.Restart();
            }
        }

        private void aboutButton_Click(object sender, EventArgs e)
        {
            AboutBox1 box = new AboutBox1();
            box.ShowDialog();
        }

        private void CreateRequiredDBs(String connectionString)
        {
            MSSqlExecutor sqlExecutor = new MSSqlExecutor(connectionString);
            String sqlPath = ApplicationDeployment.CurrentDeployment.DataDirectory;
            sqlPath = Path.Combine(sqlPath, "Resources");
            sqlOpenFileDialog.InitialDirectory = sqlPath;
            DialogResult result = sqlOpenFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                String file = sqlOpenFileDialog.FileName;
                sqlExecutor.GenerateStatements(file);
                sqlExecutor.Execute();
            }
        }

        private void sqlExecutorButton_Click(object sender, EventArgs e)
        {
            CreateRequiredDBs(appConfig.LocalConnectionString);
        }
    }
}
